﻿using System.Collections.Generic;
using Fierce.Entity;
using Fierce.Core.DbSet;
using Fierce.Core.Aop;

namespace Fierce.Core.Service
{
    /// <summary>
    /// 树形业务基类
    /// </summary>
    /// <typeparam name="T">实体类型</typeparam>
    /// <typeparam name="R">仓储接口</typeparam>
    public abstract class TreeService<T> : OrderService<T> where T : EntityTree, new()
    {
        /// <summary>
        /// 当前数据对象
        /// </summary>
        private TreeDbSet<T> dbSet
        {
            get { return db.DbSet<T, TreeDbSet<T>>(); }
        }

        /// <summary>
        /// 根据主键获取子集
        /// </summary>
        /// <param name="key">键值</param>
        /// <returns>列表</returns>
        public virtual List<T> Children(string key)
        {
            return dbSet.Children(key);
        }

        /// <summary>
        /// 根据主键获取对象和子集
        /// </summary>
        /// <param name="key">键值</param>
        /// <returns>列表</returns>
        public virtual List<T> EntityAndChildren(string key)
        {
            return dbSet.EntityAndChildren(key);
        }

        /// <summary>
        /// 设置排序
        /// </summary>
        /// <param name="id">编号</param>
        /// <param name="order">顺序</param>
        /// <param name="pid">父级</param>
        /// <returns>操作结果</returns>
        public virtual ServiceResult Order(string id, string order, string pid)
        {
            if (dbSet.Order(id, order, pid))
            {
                return SaveAndResult("调换成功");
            }
            return FailureResult("没有可调换目标");
        }
    }
}